package org.jeecg.modules.hqYoulYx.service.impl;

import org.jeecg.modules.hqYoulYx.entity.HqYoulYx;
import org.jeecg.modules.hqYoulYx.entity.HqYoulRk;
import org.jeecg.modules.hqYoulYx.mapper.HqYoulRkMapper;
import org.jeecg.modules.hqYoulYx.mapper.HqYoulYxMapper;
import org.jeecg.modules.hqYoulYx.service.IHqYoulYxService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 后勤油料邮箱
 * @Author: jeecg-boot
 * @Date:   2020-08-19
 * @Version: V1.0
 */
@Service
public class HqYoulYxServiceImpl extends ServiceImpl<HqYoulYxMapper, HqYoulYx> implements IHqYoulYxService {

	@Autowired
	private HqYoulYxMapper hqYoulYxMapper;
	@Autowired
	private HqYoulRkMapper hqYoulRkMapper;
	
	@Override
	@Transactional
	public void saveMain(HqYoulYx hqYoulYx, List<HqYoulRk> hqYoulRkList) {
		hqYoulYxMapper.insert(hqYoulYx);
		if(hqYoulRkList!=null && hqYoulRkList.size()>0) {
			for(HqYoulRk entity:hqYoulRkList) {
				//外键设置
				entity.setYoux(hqYoulYx.getId());
				hqYoulRkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqYoulYx hqYoulYx,List<HqYoulRk> hqYoulRkList) {
		hqYoulYxMapper.updateById(hqYoulYx);
		
		//1.先删除子表数据
		hqYoulRkMapper.deleteByMainId(hqYoulYx.getId());
		
		//2.子表数据重新插入
		if(hqYoulRkList!=null && hqYoulRkList.size()>0) {
			for(HqYoulRk entity:hqYoulRkList) {
				//外键设置
				entity.setYoux(hqYoulYx.getId());
				hqYoulRkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqYoulRkMapper.deleteByMainId(id);
		hqYoulYxMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqYoulRkMapper.deleteByMainId(id.toString());
			hqYoulYxMapper.deleteById(id);
		}
	}
	
}
